import 'package:flutter/material.dart';
import 'package:item_detail_demo/item_detail/model/item_detail_do.dart';
import 'package:provider/provider.dart';

import '../manager/item_detail_data_manager.dart';

class ItemPrice extends StatefulWidget {
  const ItemPrice({Key? key}) : super(key: key);

  @override
  State<ItemPrice> createState() => _ItemPriceState();
}

class _ItemPriceState extends State<ItemPrice> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildPriceWidget(context),
          _buildDesc(context),
        ],
      ),
    );
  }

  RichText _buildPriceWidget(BuildContext context) {
    final ItemDetailDO itemDetailDO = Provider.of<ItemDetailDataManager>(context).itemDetailDO;
    final List<TextSpan> spans = <TextSpan>[
      const TextSpan(
          text: "¥",
          style: TextStyle(
              color: Color(0xFFFF4444),
              fontSize: 18.0,
              letterSpacing: 2.0,
              fontWeight: FontWeight.bold)
      ),
      TextSpan(
          text: itemDetailDO.price,
          style: const TextStyle(
              color: Color(0xFFFF4444),
              fontSize: 24.0,
              fontWeight: FontWeight.bold)
      ),
      const TextSpan(
          text: " | ",
          style: TextStyle(
              color: Color(0xFF333333),
              fontSize: 14.0,
              fontWeight: FontWeight.normal)
      ),
      TextSpan(
          text: itemDetailDO.tag,
          style: const TextStyle(
              color: Color(0xFF0D9EFF),
              fontSize: 14.0,
              fontWeight: FontWeight.normal)
      ),
    ];
    return RichText(
      textAlign: TextAlign.end,
      text: TextSpan(
        children: spans,
      ),
    );
  }

  Widget _buildDesc(BuildContext context) {
    return Row(
      children: [
        _buildTag(context),
        Expanded(child: Container(),),
        Text(Provider.of<ItemDetailDataManager>(context).itemDetailDO.itemRecord,
          style: const TextStyle(fontSize: 12.0, color: Color(0xFFA3A3A3), fontWeight: FontWeight.normal),
          maxLines: 1,
        ),
      ],
    );
  }

  Widget _buildTag(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(3.0),
        border: Border.all(color: const Color(0x7FFF4400), width: 1),
        color: const Color(0xFFFFFFFF),
      ),
      height: 20,
      margin: const EdgeInsets.only(top: 4, left: 4),
      padding: const EdgeInsets.only(left: 4, right: 4),
      child: Text(
        Provider.of<ItemDetailDataManager>(context).itemDetailDO.priceTag,
        style: const TextStyle(
            color: Color(0xFFFF4400),
            fontSize: 12,
            fontWeight: FontWeight.normal,
            height: 1.39),
      ),
    );
  }
}
